parse out memory usage data

chris (2004-05-03 15:36:59)
2707 views
0 replies
We were fiddling with snmp and mrtg in the office the other day. A colleague of mine needed a quick script to give him data on total memory and total free, then total swap and total free. It was a 5 minute job and here's the simple solution. (linux only).

#!/usr/bin/perl
use strict;

my $result=`cat /proc/meminfo`;
my @results=split("\n",$result);

# iterate and copy each one
foreach (@results){
chomp;
$_ =~ /^MemTotal:\s+(\d+)/i && print "$1\n";
$_ =~ /^MemFree:\s+(\d+)/i && print "$1\n";
$_ =~ /^SwapTotal:\s+(\d+)/i && print "$1\n";
$_ =~ /^SwapFree:\s+(\d+)/i && print "$1\n";
}


christo
comment